package ex.com


import groovy.lang.Closure

import org.hibernate.ScrollMode


class ProductController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def grailsApplication
	def productService
	def m1Service

	def t = {
		Product.list()*.delete(failOnError:true)

		(1..20000).each{
			def nm = it%2==0 ? "odd":"even"
			new Product(price:it, name:nm+it.toString()).save(failOnError:true)
			println  it
		}
		render "t"
	}

	def index = {
//		def c = Product.createCriteria()
//		Double d  = 0
//		def query  = {
////			gt("price", d)
//			priceGt(0)
//		}
//		c.list(max:10, query)
		redirect(action: "list", params: params)
	}

	def time(Closure collable){
		def start = System.currentTimeMillis();
		collable.call()
		def end = System.currentTimeMillis();

		def ret = end-start
		println ret
		return ret
	}

	def listEach = {
		def all = 0

		def t = time{
			(1..10).each{
				def l = Product.withCriteria{ like("name", "%odd%") }
				l.each{ println it.name }
				println l.size()
			}
		}
		println t
		params.max = 20
		render view:"list", model:[productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
	}

	def lists = {
		Product.withSession { s ->

			time{
				(1..10).each{
					def c = s.createCriteria(Product)
					def q = { like("name","%odd%") } 
//					c(q)
					def rs = c.scroll(ScrollMode.FORWARD_ONLY)
					while (rs.next()) {
						println rs.get(0).name
					}
				}
			}
		}


		render view:"list", model:[productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
	}

	def list = {

		def t = time{
			(1..10).each{
				def q = {
					like("name", "%odd%")
					//					cache false
					maxResults(100)
					//			max(10)
				}
				def c = Product.createCriteria()
				c(q)
				def rs = c.scroll(ScrollMode.FORWARD_ONLY)

				while(rs.next()){
					println rs.get(0).name
				}
				println rs.getRowNumber()
			}
		}
		println t

		//		def start = System.currentTimeMillis();
		//		while(rs.next()){
		//			//			println rs.get(0)
		//			println rs.get(0).name
		//		}
		//		def end = System.currentTimeMillis();
		//
		//		println end-start

		params.max = 20
		[productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
	}

	def create = {
		def productInstance = new Product()
		productInstance.properties = params
		return [productInstance: productInstance]
	}

	def save = {
		def productInstance = new Product(params)
		if (productInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])}"
			redirect(action: "show", id: productInstance.id)
		}
		else {
			render(view: "create", model: [productInstance: productInstance])
		}
	}

	def show = {
		def productInstance = Product.get(params.id)
		if (!productInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])}"
			redirect(action: "list")
		}
		else {
			[productInstance: productInstance]
		}
	}

	def edit = {
		def productInstance = Product.get(params.id)
		if (!productInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [productInstance: productInstance]
		}
	}

	def update = {
		def productInstance = Product.get(params.id)
		if (productInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (productInstance.version > version) {

					productInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'product.label', default: 'Product')]
					as Object[], "Another user has updated this Product while you were editing")
					render(view: "edit", model: [productInstance: productInstance])
					return
				}
			}
			productInstance.properties = params
			if (!productInstance.hasErrors() && productInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])}"
				redirect(action: "show", id: productInstance.id)
			}
			else {
				render(view: "edit", model: [productInstance: productInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])}"
			redirect(action: "list")
		}
	}

	def delete = {
		def productInstance = Product.get(params.id)
		if (productInstance) {
			try {
				productInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'product.label', default: 'Product'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'product.label', default: 'Product'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), params.id])}"
			redirect(action: "list")
		}
	}
}
